{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在实验三的基础上继续： <br>\n",
    "输入文件为yq_in_04.txt，输出文件yq_out_04.txt,但有如下要求： <br>\n",
    "每个省后面有一个总数; <br>\n",
    "输出省按总数从大到小排序；如果两个省总数一样，按拼音（字母）排序； <br>\n",
    "每个省内各市从大到小排序；如果两个市总数一样，按拼音（字母）排序； <br>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 打开yq_in.txt文件\n",
    "yq_in = 'yq_in_04.txt'\n",
    "f_in = open(yq_in, 'r', encoding = 'gb2312') \n",
    "# 将文件内内容写入file\n",
    "file = f_in.read()\n",
    "f_in.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "list0=[]\n",
    "simple = []\n",
    "flag=0\n",
    "simple.append(file.split())  # file里的全部元素作为一个列表集合放入simple这个列表里\n",
    "for i in simple[0]:   # 遍历simple列表里第0个元素\n",
    "    list0.append(i)  # 将其添加到list0中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['浙江省', '温州', '504', '浙江省', '台州', '146', '浙江省', '省十里丰监狱', '36', '浙江省', '杭州', '169', '浙江省', '宁波', '157', '浙江省', '嘉兴', '45', '浙江省', '金华', '55', '浙江省', '绍兴', '42', '浙江省', '舟山', '10', '浙江省', '丽水', '17', '浙江省', '衢州', '14', '浙江省', '湖州', '10', '江西省', '九江', '118', '江西省', '南昌', '229', '江西省', '上饶', '123', '江西省', '新余', '130', '江西省', '赣州', '76', '江西省', '萍乡', '33', '江西省', '宜春', '106', '江西省', '抚州', '72', '江西省', '吉安', '22', '江西省', '鹰潭', '18', '江西省', '景德镇', '6', '江西省', '赣江新区', '1', '广东省', '深圳', '417', '广东省', '广州', '346', '广东省', '东莞', '97', '广东省', '佛山', '84', '广东省', '珠海', '98', '广东省', '江门', '23', '广东省', '中山', '66', '广东省', '惠州', '62', '广东省', '湛江', '22', '广东省', '茂名', '14', '广东省', '肇庆', '19', '广东省', '梅州', '16', '广东省', '阳江', '14', '广东省', '汕头', '25', '广东省', '韶关', '10', '广东省', '揭阳', '8', '广东省', '汕尾', '5', '广东省', '潮州', '5', '广东省', '清远', '12', '广东省', '河源', '4', '广东省', '待明确地区', '0', '江苏省', '南京', '93', '江苏省', '苏州', '87', '江苏省', '淮安', '66', '江苏省', '连云港', '48', '江苏省', '徐州', '79', '江苏省', '无锡', '55', '江苏省', '扬州', '23', '江苏省', '泰州', '37', '江苏省', '常州', '51', '江苏省', '南通', '40', '江苏省', '盐城', '27', '江苏省', '宿迁', '13', '江苏省', '镇江', '12', '湖南省', '长沙', '242', '湖南省', '岳阳', '156', '湖南省', '株洲', '80', '湖南省', '娄底', '76', '湖南省', '常德', '82', '湖南省', '邵阳', '102', '湖南省', '衡阳', '48', '湖南省', '湘潭', '36', '湖南省', '郴州', '39', '湖南省', '益阳', '59', '湖南省', '永州', '44', '湖南省', '怀化', '40', '湖南省', '湘西自治州', '8', '湖南省', '张家界', '5', '安徽省', '阜阳', '155', '安徽省', '合肥', '174', '安徽省', '蚌埠', '160', '安徽省', '六安', '69', '安徽省', '亳州', '108', '安徽省', '马鞍山', '38', '安徽省', '宿州', '41', '安徽省', '安庆', '83', '安徽省', '铜陵', '29', '安徽省', '淮南', '27', '安徽省', '芜湖', '33', '安徽省', '淮北', '27', '安徽省', '池州', '17', '安徽省', '滁州', '13', '安徽省', '黄山', '9', '安徽省', '宣城', '6', '陕西省', '西安', '120', '陕西省', '汉中', '26', '陕西省', '安康', '26', '陕西省', '渭南', '15', '陕西省', '铜川', '8', '陕西省', '商洛', '7', '陕西省', '咸阳', '17', '陕西省', '宝鸡', '13', '陕西省', '延安', '8', '陕西省', '榆林', '3', '陕西省', '韩城', '1', '陕西省', '杨凌', '1', '陕西省', '待明确地区', '0', '河南省', '信阳', '274', '河南省', '郑州', '157', '河南省', '南阳', '156', '河南省', '周口', '76', '河南省', '驻马店', '139', '河南省', '平顶山', '58', '河南省', '安阳', '53', '河南省', '漯河', '35', '河南省', '开封', '26', '河南省', '商丘', '91', '河南省', '许昌', '39', '河南省', '濮阳', '17', '河南省', '洛阳', '31', '河南省', '新乡', '57', '河南省', '鹤壁', '19', '河南省', '焦作', '32', '河南省', '济源', '5', '河南省', '三门峡', '7', '河南省', '待明确地区', '0', '贵州省', '遵义', '32', '贵州省', '贵阳', '36', '贵州省', '毕节', '23', '贵州省', '黔南州', '17', '贵州省', '六盘水', '10', '贵州省', '铜仁', '10', '贵州省', '黔东南州', '10', '贵州省', '安顺', '4', '贵州省', '黔西南州', '4']\n"
     ]
    }
   ],
   "source": [
    "print(list0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "浙江省\n",
      "温州\t504\n",
      "台州\t146\n",
      "省十里丰监狱\t36\n",
      "杭州\t169\n",
      "宁波\t157\n",
      "嘉兴\t45\n",
      "金华\t55\n",
      "绍兴\t42\n",
      "舟山\t10\n",
      "丽水\t17\n",
      "衢州\t14\n",
      "湖州\t10\n",
      "总数为： 1205\n",
      "--------------------------------------------------\n",
      "江西省\n",
      "九江\t118\n",
      "南昌\t229\n",
      "上饶\t123\n",
      "新余\t130\n",
      "赣州\t76\n",
      "萍乡\t33\n",
      "宜春\t106\n",
      "抚州\t72\n",
      "吉安\t22\n",
      "鹰潭\t18\n",
      "景德镇\t6\n",
      "赣江新区\t1\n",
      "总数为： 934\n",
      "--------------------------------------------------\n",
      "广东省\n",
      "深圳\t417\n",
      "广州\t346\n",
      "东莞\t97\n",
      "佛山\t84\n",
      "珠海\t98\n",
      "江门\t23\n",
      "中山\t66\n",
      "惠州\t62\n",
      "湛江\t22\n",
      "茂名\t14\n",
      "肇庆\t19\n",
      "梅州\t16\n",
      "阳江\t14\n",
      "汕头\t25\n",
      "韶关\t10\n",
      "揭阳\t8\n",
      "汕尾\t5\n",
      "潮州\t5\n",
      "清远\t12\n",
      "河源\t4\n",
      "待明确地区\t0\n",
      "总数为： 1347\n",
      "--------------------------------------------------\n",
      "江苏省\n",
      "南京\t93\n",
      "苏州\t87\n",
      "淮安\t66\n",
      "连云港\t48\n",
      "徐州\t79\n",
      "无锡\t55\n",
      "扬州\t23\n",
      "泰州\t37\n",
      "常州\t51\n",
      "南通\t40\n",
      "盐城\t27\n",
      "宿迁\t13\n",
      "镇江\t12\n",
      "总数为： 631\n",
      "--------------------------------------------------\n",
      "湖南省\n",
      "长沙\t242\n",
      "岳阳\t156\n",
      "株洲\t80\n",
      "娄底\t76\n",
      "常德\t82\n",
      "邵阳\t102\n",
      "衡阳\t48\n",
      "湘潭\t36\n",
      "郴州\t39\n",
      "益阳\t59\n",
      "永州\t44\n",
      "怀化\t40\n",
      "湘西自治州\t8\n",
      "张家界\t5\n",
      "总数为： 1017\n",
      "--------------------------------------------------\n",
      "安徽省\n",
      "阜阳\t155\n",
      "合肥\t174\n",
      "蚌埠\t160\n",
      "六安\t69\n",
      "亳州\t108\n",
      "马鞍山\t38\n",
      "宿州\t41\n",
      "安庆\t83\n",
      "铜陵\t29\n",
      "淮南\t27\n",
      "芜湖\t33\n",
      "淮北\t27\n",
      "池州\t17\n",
      "滁州\t13\n",
      "黄山\t9\n",
      "宣城\t6\n",
      "总数为： 989\n",
      "--------------------------------------------------\n",
      "陕西省\n",
      "西安\t120\n",
      "汉中\t26\n",
      "安康\t26\n",
      "渭南\t15\n",
      "铜川\t8\n",
      "商洛\t7\n",
      "咸阳\t17\n",
      "宝鸡\t13\n",
      "延安\t8\n",
      "榆林\t3\n",
      "韩城\t1\n",
      "杨凌\t1\n",
      "待明确地区\t0\n",
      "总数为： 245\n",
      "--------------------------------------------------\n",
      "河南省\n",
      "信阳\t274\n",
      "郑州\t157\n",
      "南阳\t156\n",
      "周口\t76\n",
      "驻马店\t139\n",
      "平顶山\t58\n",
      "安阳\t53\n",
      "漯河\t35\n",
      "开封\t26\n",
      "商丘\t91\n",
      "许昌\t39\n",
      "濮阳\t17\n",
      "洛阳\t31\n",
      "新乡\t57\n",
      "鹤壁\t19\n",
      "焦作\t32\n",
      "济源\t5\n",
      "三门峡\t7\n",
      "待明确地区\t0\n",
      "总数为： 1272\n",
      "--------------------------------------------------\n",
      "贵州省\n",
      "遵义\t32\n",
      "贵阳\t36\n",
      "毕节\t23\n",
      "黔南州\t17\n",
      "六盘水\t10\n",
      "铜仁\t10\n",
      "黔东南州\t10\n",
      "安顺\t4\n",
      "黔西南州\t4\n",
      "总数为： 146\n"
     ]
    }
   ],
   "source": [
    "new_str = ''\n",
    "j = 0\n",
    "num = 0\n",
    "help_list = []\n",
    "\n",
    "for i in list0:\n",
    "    if(j == 0 ):\n",
    "        limit = i\n",
    "        print(i, end='\\n')\n",
    "#         help_list.append(i)\n",
    "    else:\n",
    "        if(i == limit or list0[j-1] == limit or list0[j-2] == limit):\n",
    "            if(i == limit):  # 省份\n",
    "                pass\n",
    "            elif(list0[j-1] == limit):  # 城市\n",
    "                print(i, end='\\t')\n",
    "            elif(list0[j-2] == limit):  # 数量\n",
    "                i = int(i)\n",
    "                num += i\n",
    "                print(i, end='\\n')\n",
    "        else:\n",
    "            limit = i\n",
    "#             help_list.append(i)\n",
    "            print('总数为：', num)\n",
    "            num = 0  # 总数重置清零\n",
    "            print('-'*50)\n",
    "            print(i, end='\\n')\n",
    "    j+=1\n",
    "print('总数为：', num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "help_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'yq_out' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-8b68420ac630>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0miyq_out\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'yq_out_04.txt'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mf_out\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0myq_out\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"w+\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mf_out\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnew_str\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mf_out\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'yq_out' is not defined"
     ]
    }
   ],
   "source": [
    "iyq_out = 'yq_out_04.txt'\n",
    "f_out=open(yq_out, \"w+\")\n",
    "f_out.write(new_str)\n",
    "f_out.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
